package com.lgc.kt_jetpack_android_edu

import android.view.MenuItem
import android.widget.Adapter
import androidx.core.view.forEachIndexed
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.lifecycle.MediatorLiveData
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.navigation.NavigationBarView
import com.lgc.common.base.BaseActivity
import com.lgc.common.widget.NavVp2Mediator
import com.lgc.course.CourseFragment
import com.lgc.home.HomeFragment
import com.lgc.kt_jetpack_android_edu.databinding.ActivityMainBinding
import com.lgc.mine.MineFragment
import com.lgc.study.StudyFragment

//import com.lgc.kt_jetpack_android_edu.base.BaseActivity


/**
 * @author lgc
 * @description
 * @date 2024/7/29
 * 项目代码github地址：https://github.com/zhiwei1990/android-jetpack-demo
 * 这是一个基于kotlin+jetpack+android语言的教育类型的android项目
 * jetpack官方网站:https://developer.android.com/jetpack
 * 简书kotlin学习:https://www.jianshu.com/p/bdbe2ab6e9b2
 * 练习接口地址:https://course.api.cniao5.com
 *             appid:tcvEYUlWEc
 *             appsecret:J#y9sJesv*5HmqLqEV 1 yUPYfpH$pHx$!
 *  项目接口:http://yapi.54yct.com/project/24/interface/api
 *
 *
 */
class MainActivity : BaseActivity<ActivityMainBinding>() {
    //    private lateinit var navController:NavController
    override fun getLayoutRes(): Int {
        return R.layout.activity_main
    }
    val data = MediatorLiveData<String>()
    //初始化所有的fragment存放于集合中
    private val fragments: MutableMap<Int, Fragment> = mutableMapOf(
        INDEX_HOME to HomeFragment(),
        INDEX_COURSE to CourseFragment(),
        INDEX_STUDY to StudyFragment(),
        INDEX_MINE to MineFragment(),
    )
    override fun initConfig() {
        super.initConfig()
    }
    override fun initView() {
        super.initView()
        mBinding.apply {
            //BottomNavigationView+ViewPager2+fragment配合使用
            vp2Main.adapter=MainViewPagerAdapter(this@MainActivity,fragments)
            NavVp2Mediator(navView,vp2Main) { navView, vp2Main ->
                        vp2Main.isUserInputEnabled = false //禁止用户滑动
            }.attach()
        }
    }
    companion object{
        const val INDEX_HOME=0//首页home对应的索引位置
        const val INDEX_COURSE=1//课程course对应的索引位置
        const val INDEX_STUDY=2//学习study对应的索引位置
        const val INDEX_MINE=3//我的mine对应的索引位置
    }
    /**
     * 创建viewPager2的适配器
     */
    class MainViewPagerAdapter(fragmentActivity:FragmentActivity,val frgaments: MutableMap<Int,Fragment>):FragmentStateAdapter(fragmentActivity){
        override fun getItemCount(): Int =frgaments.size

        override fun createFragment(position: Int)=frgaments[position] ?: error("未找到对应的Fragment")
        }
    }


//    override fun initData() {
//        super.initData()
        //以下是使用Jetpack原有的Navigation方式导航
//        val navView: BottomNavigationView = mBinding.navView
//         navController = findNavController(R.id.nav_host_fragment_activity_main)
//        val appBarConfiguration = AppBarConfiguration(
//            setOf(
//                R.id.homeFragment,
//                R.id.courseFragment,
//                R.id.studyFragment,
//                R.id.mineFragment
//            )
//        )
//        setupActionBarWithNavController(navController, appBarConfiguration)
//        navView.setupWithNavController(navController)
//    }

    //actionBar联动的返回
/*    override fun onSupportNavigateUp(): Boolean {
        return if(navController.navigateUp()){true}else{super.onSupportNavigateUp()}
    }*/

    //另一种获取viewBinding的方式
//    override fun getViewBinding(): ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)
//}

//类型别名定义
typealias ReFragment=()-> Fragment